package com.kede.app.service.impl;

import com.kede.app.domain.Transport;
import com.kede.app.repository.TransportRepository;
import com.kede.app.service.PackageService;
import com.kede.app.service.TransportService;
import com.kede.app.service.dto.TransportDto;
import com.kede.app.service.dto.TransportQueryCriteria;
import com.kede.app.service.mapstruct.TransportMapper;
import com.kede.utils.FileUtil;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import com.kede.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-10-14
**/
@Service
@RequiredArgsConstructor
public class TransportServiceImpl implements TransportService {

    private final TransportRepository transportRepository;
    private final TransportMapper transportMapper;
    private final PackageService packageService;
//    private final

    @Override
    public Map<String,Object> queryAll(TransportQueryCriteria criteria, Pageable pageable){
        Page<Transport> page = transportRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(transportMapper::toDto));
    }

    @Override
    public List<TransportDto> queryAll(TransportQueryCriteria criteria){
        return transportMapper.toDto(transportRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TransportDto findById(Integer id) {
        Transport transport = transportRepository.findById(id).orElseGet(Transport::new);
        ValidationUtil.isNull(transport.getId(),"Transport","id",id);
        return transportMapper.toDto(transport);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TransportDto create(Transport resources) {
        return transportMapper.toDto(transportRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Transport resources) {
        Transport transport = transportRepository.findById(resources.getId()).orElseGet(Transport::new);
        ValidationUtil.isNull( transport.getId(),"Transport","id",resources.getId());
        transport.copy(resources);
        transportRepository.save(transport);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            transportRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TransportDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TransportDto transport : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("联系人电话", transport.getPhone());
            map.put("联系人", transport.getPeople());
            map.put("始发地", transport.getStartLocation());
            map.put("目的地", transport.getEndLocation());
            map.put("当前位置", transport.getCurLocation());
            map.put("车牌号", transport.getCarNo());
            map.put("发车时间", transport.getStartTime());
            map.put("创建时间", transport.getCreateTime());
            map.put("众多包裹编号,分割", transport.getPackages());
            map.put("司机的openid", transport.getOpenid());
            map.put("备注信息", transport.getRemark());
            map.put("1 正常 2运输中 3已结算", transport.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

}